package com.jackrain.nea.wms.lts;

import com.github.ltsopensource.spring.TaskTrackerAnnotationFactoryBean;
import com.github.ltsopensource.tasktracker.TaskTracker;
import com.jackrain.nea.config.PropertiesConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Slf4j
@Configuration
public class TaskTrackerConfig implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Autowired
    private PropertiesConf pconf;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
    public TaskTracker taskTracker() throws Exception{
        // pconf= ApplicationContextHandle.getBean("propertiesConf");
        String REGISTRY_ADDRESS=pconf.getProperty("lts.zookeeper.server");
        TaskTrackerAnnotationFactoryBean factoryBean = new TaskTrackerAnnotationFactoryBean();
        factoryBean.setApplicationContext(applicationContext);
        factoryBean.setJobRunnerClass(JobRunnerDispatcher.class);
        factoryBean.setLocations("config/lts.properties");
        factoryBean.afterPropertiesSet();
        factoryBean.getObject().setRegistryAddress(REGISTRY_ADDRESS);
        log.debug("jobrunner addr->"+REGISTRY_ADDRESS);
        factoryBean.start();
        return factoryBean.getObject();
    }
}
